package org.itboys.core.controller.web.company;

import com.alibaba.fastjson.JSONObject;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mongo.dispatch.SundayDispatchModel;
import org.itboys.core.entity.mongo.dispatch.SundayDispatchOrder;
import org.itboys.core.entity.mysql.tray.SundayTrayModel;
import org.itboys.core.service.mongo.web.SundayDispatchService;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.service.mysql.web.SundayTrayModelService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/12/14
 * @Description：调度单
 */
@Controller
@RequestMapping("/sunday/web/company/dispatch")
public class SundayCompanyDispatchController extends BaseController {
    /**
     * 主页跳转类型
     * 1--接单
     * 2--详情
     * 3--平台审核
     */
    private static Integer index_type_default=0;
    private static Integer index_type_accept=1;
    private static Integer index_type_detail=2;

    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayDispatchService dispatchService;
    @Autowired
    private SundayTrayModelService modelService;
    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response,Model model
            , @RequestParam(value = "type", required = true) Integer type
    ,@RequestParam(value = "orderId",required = false)Long orderId) {
        if (index_type_default.equals(type)) {
            return "/sunday/company/dispatch/index";
        } else if (index_type_accept.equals(type)) {
            return "/sunday/company/dispatch/accept/index";
        }else if(index_type_detail.equals(type)){
            Map<String,Object> param=new HashMap<>();
            param.put("isDeleted",SundayCommonConstants.NO);
            List<SundayTrayModel> models = modelService.select(param);
            model.addAttribute("models", models);
            List<Long> trayIds = dispatchService.getTrays(orderId);
            if(ListUtil.isNotNull(trayIds)){
                model.addAttribute("trayIds",trayIds);
            }
            return "/sunday/company/dispatch/detail";
        }
        return "error";
    }

    @RequestMapping("/pool")
    public String pool(HttpServletRequest request, HttpServletResponse response) {
        return "/sunday/company/dispatch/pool";
    }

    @RequestMapping("/input")
    public String input(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "id", required = true) Long id, Model model
            , @RequestParam("type") Integer type) {

        Long userId = AdminSessionHolder.getAdminUserId();
        SundayMember member = memberService.getMemberByUserId(userId);

        SundayDispatchOrder order = new SundayDispatchOrder();
        if (id == 0) {
            order.setId(0L);
        } else {
            order = dispatchService.getById(id);
        }
        model.addAttribute("memberId", member.getId());
        model.addAttribute("order", order);
        if (SundayCommonConstants.dispatch_select_type_own.equals(type)) {
            return "/sunday/company/dispatch/input";
        } else if (SundayCommonConstants.dispatch_select_type_other.equals(type)) {
            return "/sunday/company/dispatch/accept/input";
        }
        return "/sunday/error";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response
            , @RequestParam(value = "type",required = false) Integer type) {
        Long userId = AdminSessionHolder.getAdminUserId();
        SundayMember member = memberService.getMemberByUserId(userId);
        PageResult<SundayDispatchOrder> result = dispatchService.select(request, member, type);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    @RequestMapping("/pool/select")
    public void pool_select(HttpServletRequest request, HttpServletResponse response) {
        Long userId = AdminSessionHolder.getAdminUserId();
        SundayMember member = memberService.getMemberByUserId(userId);
        PageResult<SundayDispatchOrder> result = dispatchService.pool(request, member);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    @RequestMapping("/addModel")
    public String addModel(HttpServletRequest request, HttpServletResponse response, Model model
            , @RequestParam("no") String no) {
        model.addAttribute("no", no);
        return "/sunday/company/dispatch/model";
    }

    @RequestMapping("/apply")
    public void apply(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("id") Long id
            , @RequestParam("applyStoreId") Long applyStoreId
            , @RequestParam("applyStoreName") String applyStoreName
            , @RequestParam(value = "models") String models) {
        List<SundayDispatchModel> dispatchModels = JSONObject.parseArray(models, SundayDispatchModel.class);
        Long userId = AdminSessionHolder.getAdminUserId();
        SundayMember member = memberService.getMemberByUserId(userId);
        dispatchService.apply(id, member, applyStoreId, applyStoreName, dispatchModels);
        ResponseMessage.success();
    }

    @RequestMapping("/accept")
    public void accept(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("id") Long id) {
        Long userId = AdminSessionHolder.getAdminUserId();
        SundayMember member = memberService.getMemberByUserId(userId);
        dispatchService.accept(member, id);
        ResponseMessage.success();
    }

    @RequestMapping("/selectModels")
    public void selectModels(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("id") Long id) {
        PageResult<SundayDispatchModel> result = dispatchService.selectModels(request, id);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    @RequestMapping("/saveTrays")
    public void saveTrays(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("id") Long id
            , @RequestParam("orderId") Long orderId
            , @RequestParam("trayIds") List<Long> trayIds) {
        dispatchService.saveTrays(orderId, id, trayIds);
        ResponseMessage.success();
    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("id") Long id) {
        dispatchService.delete(id);
        ResponseMessage.success();
    }

    @RequestMapping("/update")
    public void update(HttpServletRequest request,HttpServletResponse response
            ,@RequestParam("id")Long id
            ,@RequestParam("state")Integer state){
        dispatchService.update(id,"state",state);
        ResponseMessage.success();
    }
}
